<?php

class ArticleController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	header('Location: manager');
    }

    public function newAction()
    {
        include_once dirname(__FILE__).'./../models/CategoryModel.php';
        $categoryModel = new CategoryModel();
        $categories = $categoryModel->GetCategory();
        $maxLevel = $categoryModel->GetMaxLevel();
        
        $this->view->categories = $categories;
        $this->view->maxLevel = $maxLevel;
    }

    public function saveAction()
    {
        $postArray['title'] = $this->_request->getPost('tTitle');
        $postArray['alias'] = $this->_request->getPost('tAlias');
        $postArray['category'] = $this->_request->getPost('sCategory');
        $postArray['state'] = $this->_request->getPost('sState');
        $postArray['access'] = $this->_request->getPost('sAccess');
        $postArray['content'] = $this->_request->getPost('mycontent');
        $postArray['createdBy'] = $this->_request->getPost('tCreatedBy');
        $postArray['createdDate'] = $this->_request->getPost('tCreatedDate');
        $postArray['startPublishing'] = $this->_request->getPost('tStartPublishing');
        $postArray['finishPublishing'] = $this->_request->getPost('tFinishPublishing');
        $postArray['metaDes'] = $this->_request->getPost('tMetaDes');
        $postArray['metaKeyword'] = $this->_request->getPost('tMetaKeyword');

        $postValue = $postArray;
        
        $pos = strpos($postValue['category'],'|_');
        $postValue['category'] = substr($postValue['category'], $pos+4);
        
        switch ($postArray['state'])
        {
        	case 'Published':
        		$postValue['state']=0;
        		break;
        	case 'Unpublished':
        		$postValue['state']=1;
        		break;
        	case 'Archived':
        		$postValue['state']=2;
        		break;
        	case 'Trashed':
        		$postValue['state']=3;
        		break;
        }
        
    	switch ($postArray['access'])
        {
        	case 'Public':
        		$postValue['access']=0;
        		break;
        	case 'Registered':
        		$postValue['access']=1;
        		break;
        	case 'Special':
        		$postValue['access']=2;
        		break;
        }
        
        include_once 'utils.php';
    	$pos=strpos($postValue['content'],'<hr>');
        if($pos !=null)
        {
        	$postValue['introContent'] = substr($postValue['content'],0,$pos);
        	$postValue['introContent'] = GetTextFromHTML($postValue['introContent']);
        	$postValue['content'] = substr($postValue['content'],$pos+4);
        }
        else{
        	$postValue['introContent'] = $postValue['content'];
        	$postValue['content']="";
        }
        
        include_once dirname(__FILE__).'./../models/CategoryModel.php';
        $categoryModel = new CategoryModel();
        $categoryID = $categoryModel->GetIDFromName($postValue['category']);
        $postValue['category']=$categoryID;
        
        include_once dirname(__FILE__).'./../models/UserModel.php';
        $userModel = new UserModel();
        $userID = $userModel->GetIDFromName($postValue['createdBy']);
        $postValue['createdBy']=$userID;
        
        include_once dirname(__FILE__).'./../models/ArticleModel.php';
        $articleModel = new ArticleModel();
        $articleModel->Add($postValue['title'],$postValue['alias'],
	        $postValue['category'], $postValue['state'], $postValue['access'], 
	        $postValue['introContent'], $postValue['content'], 
	        $postValue['createdBy'],$postValue['createdDate'], 
	        $postValue['startPublishing'], $postValue['finishPublishing'],
			$postValue['metaDes'], $postValue['metaKeyword']);
    }

    public function showAction()
    {
    	include_once dirname(__FILE__).'./../models/CategoryModel.php';
        $categoryModel = new CategoryModel();
        
        include_once dirname(__FILE__).'./../models/UserModel.php';
        $userModel = new UserModel();
        
        // action body
        $categoryID = $this->_request->getParam('categoryid',-1);
        
        //Lấy các article có ID phù hợp với đã nhập trên URL.
        include_once dirname(__FILE__).'./../models/ArticleModel.php';
        $articleModel = new ArticleModel();
        $articles = $articleModel->GetArticleByCategoryID($categoryID);
        
        //Lấy tên Category, tên user từ ID.
        
        foreach ($articles as $key=>$article)
        {
        	$category = $categoryModel->GetNameFromID($article['CategoryID']);
        	$user = $userModel->GetUserFromID($article['CreatedByID']);
        	$name = $user['Name'];
        	
        	//Đổi ID thành các giá trị đọc được.
        	$articles[$key]['CategoryID']=$category;
        	$articles[$key]['CreatedByID']=$name;
        }
        $this->view->articles = $articles;
    }

    public function managerAction()
    {
    	
    	include_once dirname(__FILE__).'./../models/ArticleModel.php';
        $articleModel = new ArticleModel();
        
        include_once dirname(__FILE__).'./../models/CategoryModel.php';
        $categoryModel = new CategoryModel();
        
        include_once dirname(__FILE__).'./../models/UserModel.php';
        $userModel = new UserModel();
        
    	//Thực hiện các action trước đó
    	$strChecked = $this->_request->getPost('ListCheckboxChecked');
        $IDsChecked = explode(',',$strChecked);
        $toDo = $this->_request->getParam('todo');
        if($toDo!=null){
        	if($toDo == "publish"){
        		$articleModel->UpdateState($IDsChecked, 0);
        	}
        	else if($toDo == "unpublish"){
        		$articleModel->UpdateState($IDsChecked, 1);
        	}
        	else if($toDo == "delete"){
        		$articleModel->Delete($IDsChecked);
        	}
        }
        
        $articles = $articleModel->GetArticleByCategoryID(-1);

        //Lấy tên Category, tên user từ ID.      
        foreach ($articles as $key=>$article)
        {
        	$category = $categoryModel->GetNameFromID($article['CategoryID']);
        	$user = $userModel->GetUserFromID($article['CreatedByID']);
        	$name = $user['Name'];
        	
        	//Đổi ID thành các giá trị đọc được.
        	$articles[$key]['CategoryName']=$category;
        	$articles[$key]['CreatedByName']=$name;
        	
        	if($article['State'] == 0){
        		$articles[$key]['State'] = "Publish";
        	}
        	else{
        		$articles[$key]['State'] = "Unpublish";
        	}
        }
        $this->view->articles = $articles;
    }

    public function editAction()
    {
        include_once dirname(__FILE__).'./../models/CategoryModel.php';
        $categoryModel = new CategoryModel();
        
        include_once dirname(__FILE__).'./../models/ArticleModel.php';
        $articleModel = new ArticleModel();
        
        include_once dirname(__FILE__).'./../models/UserModel.php';
        $userModel = new UserModel();
        
        $categories = $categoryModel->GetCategory();
        $maxLevel = $categoryModel->GetMaxLevel();
        $ID = $this->_request->getParam('id');
        if($ID == null) return;
        $article = $articleModel->GetArticleByID($ID);
        
        //Chuyển từ các ID sang tên
        $user = $userModel->GetUserFromID($article['CreatedByID']);
        $article['CreatedByName']= $user['Name'];
        
        $this->view->categories = $categories;
        $this->view->maxLevel = $maxLevel;
        $this->view->article = $article;
        $this->view->ID = $ID;
    }

    public function editDoneAction()
    {
    	include_once dirname(__FILE__).'./../models/CategoryModel.php';
        $categoryModel = new CategoryModel();
        include_once dirname(__FILE__).'./../models/UserModel.php';
        $userModel = new UserModel();
        include_once dirname(__FILE__).'./../models/ArticleModel.php';
        $articleModel = new ArticleModel();
        
        $postArray['title'] = $this->_request->getPost('tTitle');
        $postArray['alias'] = $this->_request->getPost('tAlias');
        $postArray['category'] = $this->_request->getPost('sCategory');
        $postArray['state'] = $this->_request->getPost('sState');
        $postArray['access'] = $this->_request->getPost('sAccess');
        $postArray['content'] = $this->_request->getPost('mycontent');
        $postArray['createdBy'] = $this->_request->getPost('tCreatedBy');
        $postArray['createdDate'] = $this->_request->getPost('tCreatedDate');
        $postArray['startPublishing'] = $this->_request->getPost('tStartPublishing');
        $postArray['finishPublishing'] = $this->_request->getPost('tFinishPublishing');
        $postArray['metaDes'] = $this->_request->getPost('tMetaDes');
        $postArray['metaKeyword'] = $this->_request->getPost('tMetaKeyword');
        $postArray['ID'] = $this->_request->getPost('id');

        $postValue = $postArray;
        
        $pos = strpos($postValue['category'],'|_');
        $postValue['category'] = substr($postValue['category'], $pos+4);
        
        switch ($postArray['state'])
        {
        	case 'Published':
        		$postValue['state']=0;
        		break;
        	case 'Unpublished':
        		$postValue['state']=1;
        		break;
        	case 'Archived':
        		$postValue['state']=2;
        		break;
        	case 'Trashed':
        		$postValue['state']=3;
        		break;
        }
        
    	switch ($postArray['access'])
        {
        	case 'Public':
        		$postValue['access']=0;
        		break;
        	case 'Registered':
        		$postValue['access']=1;
        		break;
        	case 'Special':
        		$postValue['access']=2;
        		break;
        }
        
        include_once 'utils.php';
    	$pos=strpos($postValue['content'],'<hr>');
        if($pos !=null)
        {
        	$postValue['introContent'] = substr($postValue['content'],0,$pos);
        	$postValue['introContent'] = GetTextFromHTML($postValue['introContent']);
        	$postValue['content'] = substr($postValue['content'],$pos+4);
        }
        else{
        	$postValue['introContent'] = $postValue['content'];
        	$postValue['content']="";
        }
        
        $categoryID = $categoryModel->GetIDFromName($postValue['category']);
        $postValue['category']=$categoryID;
        
        $userID = $userModel->GetIDFromName($postValue['createdBy']);
        $postValue['createdBy']=$userID;
        
        $articleModel->Update($postValue['ID'],$postValue['title'],$postValue['alias'],
	        $postValue['category'], $postValue['state'], $postValue['access'], 
	        $postValue['introContent'], $postValue['content'], 
	        $postValue['createdBy'],$postValue['createdDate'], 
	        $postValue['startPublishing'], $postValue['finishPublishing'],
			$postValue['metaDes'], $postValue['metaKeyword']);
    }


}













